import {
    Data
} from "../../modeal/data.js"
import {
    Login
} from "../../modeal/login.js"
import {
    showModal
} from "../../utils/ui.js"

Page({

    data: {
        bgArr: [
            './images/bg02.png',
            './images/bg01.png'
        ],
        shorEntry: [{
                type: 1,
                text: "店内点餐",
                icon: "./images/eatIn.png"
            },
            {
                type: 0,
                text: "订座预约",
                icon: "./images/yuding.png"
            },
            {
                type: 2,
                text: "外卖点餐",
                icon: "./images/takeOut.png"
            }
        ],
    },

    onLoad(options) {
        this._wxLogin()
        this._getBaseConfig()
    },

    onTab(e) {
        let type = e.currentTarget.dataset.type
        wx.setStorageSync("orderType", type)
        if (type == 2) {
            // 外卖点餐  检查位置信息
            this._getLocation()

        }
    },

    async _wxLogin() {
        const temp = await Login.wxLogin()
        const res = await Login.checkLogin()
        wx.setStorageSync("userInfo", res.data)
        if (!res.data.mobile) {
            // 未注册
            // showModal("注册提示", 1, "注册后可以获得注册优惠券,节日优惠券!").then(cb => {
            //     console.log(cb)
            // })
        }
    },

    // 获取用户基本配置
    async _getBaseConfig() {
        const res = await Data.getBaseConfig()
        wx.setStorageSync("config", res.data)
    },

    _getLocation() {
        console.log(123)
        wx.getLocation({
            type: 'wgs84',
            success: res => {
                wx.setStorageSync('location', res)
                wx.navigateTo({
                    url: '/pages/shops/shops',
                })
            },
            fail: err => {
                this._getSetting()
            }
        })
    },

    // 授权
    _getSetting() {
        wx.getSetting({
            success: res => {
                if (!res.authSetting['scope.userLocation']) {
                    wx.showModal({
                        content: '前往开启位置授权,查找附近店铺',
                        cancelColor: '#6666',
                        confirmText: '前往授权',
                        confirmColor: '#4C7B6B',
                        success: c => {
                            c.confirm ? this._openSetting() : ""
                        },
                    })
                }
            }
        })
    },

    // 手动开启授权
    _openSetting() {
        wx.openSetting({
            success: res => {
                this._getLocation()
            },
            fail: err => {}
        })
    },

    // 页面方法
    onTab1(e) {
        let type = e.currentTarget.dataset.type
        // 将订单类型存储
        wx.setStorageSync("orderType", type)

        if (type == 0) {
            wx.showModal({
                content: "预约功能开发中,尽情期待!",
                showCancel: false
            })
        } else if (type == 1) {
            // 扫码点餐
            wx.scanCode({
                onlyFromCamera: true,
                success: res => {
                    let params = res.path.split('=')[1]
                    let shopId = params.split('-')[0]
                    let deskid = params.split('-')[1]

                    //根据shopid 获取店铺信息 并且更新缓存
                    this.getShopInfoAndUpdate(shopId, deskid);

                }
            })
        } else {
            wx.navigateTo({
                url: "/pages/shops/shops"
            })
        }
    },

    getShopInfoAndUpdate(shopId, deskid) {
        data.getShopInfo(shopId).then(res => {
            wx.setStorageSync('shopInfo', res)
            wx.setStorageSync('deskid', deskid)
            wx.navigateTo({
                url: '/pages/shopping/index/index'
            })
        })
    },


})